// 商品列表操作
$(function () {
  // 全选
  $(".checkBox").on("click", function () {
    var itemAll = document.querySelectorAll(".choseItem");
    console.log(itemAll);
    for (var i = 0; i < itemAll.length; i++) {
      itemAll[i].checked = this.checked;
    }
  });
  // 批量删除商品
  function batchRemove() {
    var itemAll = document.querySelectorAll(".choseItem");
    console.log(itemAll);
    for (var i = 0; i < itemAll.length; i++) {
      if (itemAll[i].checked) {
        removeGoods();
        setTableData();
      }
    }
  }
  $("#batchRemove").on("click", function () {
    batchRemove();
  });
});

// 商品列表
$(function () {
  setTableData();
});

function removeGoods(index) {
  let goodsList = JSON.parse(localStorage.getItem("goods_car"));
  console.log(goodsList);
  goodsList.splice(index, 1);
  localStorage.setItem("goods_car", JSON.stringify(goodsList));
  setTableData();
}

function minus(index){
    let goodsList=JSON.parse(localStorage.getItem("goods_car"));
    goodsList[index].num++;
    localStorage.setItem("goods_car",JSON.stringify(goodsList));
    setTableData();
}

function plus(index){
    let goodsList=JSON.parse(localStorage.getItem("goods_car"));
    if(goodsList[index].num>1){
        goodsList[index].num--;
        localStorage.setItem("goods_car",JSON.stringify(goodsList));
        setTableData();
    }
}

function setTableData() {
  let goodsList = JSON.parse(localStorage.getItem("goods_car"));
  cartItem = "";
  let numTotal =0;
  let priceTotal = 0;
  if (goodsList && goodsList.length > 0) {
    goodsList.forEach((item, index) => {
      numTotal +=eval(item.num);
      priceTotal += eval(item.num * item.price);
      cartItem += 
        "<ul>" + 
        "<li><input type='checkbox' class='choseItem'></li>"+
        "<li><img src='"+item.imgsmall[0]+"' alt=''></li>"+
        "<li><a href=''>"+item.title+"</a></li>"+
        "<li>￥"+ item.price+"</li>"+
        "<li>"+
        "<button class='jian' onclick='plus("+index+")'>-</button>"+
        "<input type='text' value='"+(item.num)+"' id='value'>"+
        "<button class='jia' onclick='minus("+index+")'>+</button>"+
        "</li>"+
        "<li>￥"+(item.price*(item.num?item.num:1))+"</li>"+
        "<li><a href='' onclick='removeGoods("+index+")'>删除</a></li>"+
        "</ul>";                 
    });
  }else{
    cartItem = "<div>购物车是空的！！！喜欢的话把商品添加进来吧~</div>";
    console.log("购物车为空");
  }
  console.log(parseInt(numTotal),parseInt(priceTotal));
  $("#totalValue").text(parseInt(priceTotal));
  $("#numTotal").text(parseInt(numTotal));
  $(".cart-item").empty();
  $(".cart-item").append(cartItem);
  
}

function getUrlParams(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  var r = window.location.search.substr(1).match(reg);
  if (r != null) return unescape(r[2]);
  return null;
}

$(function(){
    $("#settlementAmount").on("click",function(){
        layer.confirm("您确定要结算吗?",{
            btn:["确定","取消"]
        },function(){
            layer.msg("结算成功",{
                icon:1,
            });
        });
    });
})


